package creditsProject.client.manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JFrame;

import creditsProject.client.userOfABank.UserGUI;
import creditsProject.dto.Credit;
import creditsProject.dto.RequestDto;
import creditsProject.dto.User;
import creditsProject.server.ServerThread;

public class ClientManager {
	public static void main(String[] args) {

		try {
			Socket socket = new Socket(InetAddress.getLocalHost(), 32167);
			System.out.println("connected");

			JFrame fr = new ManagerGUI();
			fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			fr.setVisible(true);

			try (ObjectInputStream ois = new ObjectInputStream(
					socket.getInputStream());
					ObjectOutputStream oos = new ObjectOutputStream(
							socket.getOutputStream());) {
				z: while (true) {

					String com = ManagerGUI.getCommand();

					if (com != null) {
						switch (com) {
						case "LOGIN":
							System.out.println("login");
							oos.writeObject(new String("LOGIN"));
							oos.flush();
							oos.writeObject(ManagerGUI.getLogin());
							oos.flush();

							String response = (String) ois.readObject();

							x: if (response != null) {

								switch (response) {
								case "INVALID":
									// System.out.println("invalid login");
									ManagerGUI
											.setLabel("Invalid phone or password SERVER");
									ManagerGUI.setLogin(null);
									oos.flush();
									break x;

								case "LOGINOK":
									System.out.println("LOGIN OK");
									ManagerGUI.setLabel("");
									fr.setVisible(false);
									ManagerGUI mg = new ManagerGUI();
									mg.ManagerMenu();
									mg.setVisible(true);
									break x;

								default:
									break;
								}
							}
							ManagerGUI.setCommand("null");
							break;
						case "ADDUSER":
							User user = ManagerGUI.getUser();
							oos.writeObject(new String("ADDUSER"));
							oos.flush();
							oos.writeObject(user);
							oos.flush();

							response = (String) ois.readObject();
							x1: if (response != null) {
								switch (response) {
								case "USEREXIST":
									ManagerGUI
											.setLabel("Such User already exist");
									break x1;
								case "ADDEDOK":
									System.out.println("Added");
									ManagerGUI
											.setLabel("User created! Genereted password is "
													+ (String) ois.readObject());
									break x1;
								}
							}
							ManagerGUI.setCommand("null");
							break;
						case "GETREQUESTS":
							ManagerGUI.setLabel("");
							oos.writeObject(new String("GETREQUESTS"));
							oos.flush();
							List<RequestDto> coll = (List<RequestDto>) ois
									.readObject();
							ManagerGUI.setRequests(coll);
							ManagerGUI mg2 = new ManagerGUI();
							mg2.ShowRequest();
							mg2.setVisible(true);
							ManagerGUI.setCommand("null");
							break;
						case "APROVEREQUEST":
							oos.writeObject(new String("APROVEREQUEST"));
							oos.flush();
							List<Integer> collId = new ArrayList<Integer>();
							collId = ManagerGUI.getIdRequests();
							oos.writeObject(new ArrayList<>(collId));
							oos.flush();
							ManagerGUI.clearIdRequests();
							ManagerGUI.setCommand("null");
							ManagerGUI.setLabelMenu("Selected requests approved");
							break;
						case "DECLINEREQUEST":
							oos.writeObject(new String("DECLINEREQUEST"));
							oos.flush();
							List<Integer> declId = new ArrayList<Integer>();
							declId = ManagerGUI.getIdRequests();
							oos.writeObject(new ArrayList<>(declId));
							oos.flush();
							ManagerGUI.clearIdRequests();
							ManagerGUI.setCommand("null");
							ManagerGUI.setLabelMenu("Selected requests declined");
							break;
						case "GETUSERHISTORY":
							ManagerGUI.setLabel("");
							List<Integer> userPhone = new ArrayList<Integer>();
							userPhone = ManagerGUI.getUserPhones();
							System.out.println(userPhone.size());
							if (userPhone.size() != 1) {
								ManagerGUI
										.setLabel("You can see history of only 1 user at time");
								ManagerGUI.setCommand("null");
								ManagerGUI.clearUserPhones();
								break;
							} else {
								oos.writeObject(new String("GETHISTORY"));
								oos.flush();
								oos.writeObject(new Integer(userPhone.get(0)));
								oos.flush();
								List<RequestDto> req = (List<RequestDto>) ois
										.readObject();
								ManagerGUI.setRequests(req);
								ManagerGUI ug3 = new ManagerGUI();
								ug3.ShowHistory();
								ug3.setVisible(true);
								ManagerGUI.setCommand("null");
								ManagerGUI.clearUserPhones();
								break;
							}
						case "ADDCREDIT":
							Credit credit = ManagerGUI.getCredit();
							oos.writeObject(new String("ADDCREDIT"));
							oos.flush();
							oos.writeObject(credit);
							oos.flush();
							ManagerGUI.setCommand("null");
							break;
						case "BYE":
							// System.out.println(ManagerGUI.getCommand());
							oos.writeObject(new String("BYE"));
							oos.flush();

							break z;

						default:
							break;

						}
					}
				}

			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}

	}
}
